package com.example.application1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private Fragment learningFragment, userFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("语言学习");

        tabLayout = findViewById(R.id.tl_main);
        learningFragment = new LearningFragment();
        userFragment = new UserFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fl_container, learningFragment, "learningFragment").commitAllowingStateLoss();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.d("what", "onTabSelected: we selected " + tab.getText());
                if (tab.getText().equals("学习")) {
                    getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, learningFragment, "learningFragment").commitAllowingStateLoss();
                }
                else {
                    getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, userFragment, "userFragment").commitAllowingStateLoss();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // do nothing
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // do nothing
            }
        });
    }
}